package com.crowntit.infrastructure;

import com.crowntit.types.annotations.DCCValue;
import com.crowntit.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DCCService {

    @DCCValue("deGradeSwitch:0")
    private String deGradeSwitch;

    @DCCValue("cutRange:10")
    private String cutRange;

    @DCCValue("scBlackList:s02c02")
    private String scBlackList;

    @DCCValue("cacheOpenSwitch:0")
    private String cacheOpenSwitch;

    public boolean isDegrade(){
        return "1".equals(deGradeSwitch);
    }

    public boolean isCutRange(String userId){
        int hashCode = Math.abs(userId.hashCode());
        int lastTwoDigits = hashCode % 100;

        if (lastTwoDigits <= Integer.parseInt(cutRange)){
            return true;
        }
        return false;
    }

    public boolean isBlackIntercept(String source, String channel){
        List<String> blackList = Arrays.asList(scBlackList.split(Constants.SPLIT));
        return blackList.contains(source + channel);
    }

    /**
     * 是否开启缓存查询，0 / true为开启，1 / false为关闭
     */
    public boolean isCacheOpenSwitch(){
        return "0".equals(cacheOpenSwitch);
    }
}
